package com.springboot_gradrecruit.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.springboot_gradrecruit.common.PageResponse;
import com.springboot_gradrecruit.common.R;
import com.springboot_gradrecruit.model.dto.interview.AddInterview;
import com.springboot_gradrecruit.model.dto.interview.PageInterviewRequest;
import com.springboot_gradrecruit.model.entity.Interview;
import com.springboot_gradrecruit.model.vo.interview.InterviewVO;
import com.springboot_gradrecruit.service.InterviewService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author zr
 */
@Tag(name = "面试相关接口")
@RestController
@RequestMapping("/interview")
public class InterviewController {

    @Resource
    private InterviewService interviewService;

    @Operation(summary = "添加面试")
    @PostMapping("/addInterview")
    public R<Boolean> addNew(@RequestBody AddInterview addInterview) {
        //1. 参数校验
        if(addInterview==null){
            throw new RuntimeException("参数为空");
        }
        //2. 调用service完成添加
        String interviewUrl = addInterview.getInterviewUrl();
        Date interviewData = addInterview.getInterviewData();
        String interviewDesc = addInterview.getInterviewDesc();
        Long interviewEnterpriseId = addInterview.getInterviewEnterpriseId();
        Long interviewStudentId = addInterview.getInterviewStudentId();
        Long interviewJobId = addInterview.getInterviewJobId();
        Interview interview = new Interview();
        interview.setInterviewIsOffer("未发出");
        interview.setInterviewStatus("未面试");
        interview.setInterviewUrl(interviewUrl);
        interview.setInterviewDesc(interviewDesc);
        interview.setInterviewData(interviewData);
        interview.setInterviewEnterpriseId(interviewEnterpriseId);
        interview.setInterviewStudentId(interviewStudentId);
        interview.setInterviewJobId(interviewJobId);

        return interviewService.save(interview)?R.success(true,"发出面试成功"):R.error(false,"添加失败");
    }

    @Operation(summary = "分页获取面试消息")
    @PostMapping("/pageInterview")
    public R<PageResponse<InterviewVO>> pageInterview(@RequestBody PageInterviewRequest pageInterviewRequest){

        //1. 校验参数
        if(pageInterviewRequest==null){
            throw new RuntimeException("参数为空");
        }
        //2. 调用service完成查询
        PageResponse<InterviewVO> pageResponse=interviewService.pageInterview(pageInterviewRequest);
        return R.success(pageResponse,"查询成功");
    }

    @Operation(summary = "企业-分页获取面试消息")
    @PostMapping("/pageInterviewByEnterprise")
    public R<PageResponse<InterviewVO>> pageInterviewByEnterprise(@RequestBody PageInterviewRequest pageInterviewRequest){

        //1. 校验参数
        if(pageInterviewRequest==null){
            throw new RuntimeException("参数为空");
        }
        //2. 调用service完成查询
        PageResponse<InterviewVO> pageResponse=interviewService.pageInterviewByEnterprise(pageInterviewRequest);
        return R.success(pageResponse,"查询成功");
    }

    @Operation(summary = "查询学生面试数量ByStudentId")
    @PostMapping("/queryInterviewCount")
    public R<Long> queryInterviewCount(Long studentId){
        //1.参数校验
        if(studentId==null){
            throw new RuntimeException("参数为空");
        }
        //2.调用service完成查询
        QueryWrapper<Interview> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("interview_student_id",studentId);
        queryWrapper.eq("interview_status","未面试");

        return R.success(interviewService.count(queryWrapper),"查询成功");


    }

    @Operation(summary = "查询面试详情")
    @PostMapping("/queryInterviewById")
    public R<InterviewVO> queryInterviewById(Long interviewId){
        //1.参数校验
        if(interviewId==null){
            throw new RuntimeException("参数为空");
        }
        //2.调用service完成查询
        InterviewVO interviewVO=interviewService.queryInterviewById(interviewId);
        return R.success(interviewVO,"查询成功");
    }

    @Operation(summary = "更新面试状态")
    @PostMapping("/updateInterviewIsOffer")
    public R<Boolean> updateInterviewIsOffer(Long interviewId,String interviewIsOffer){
        //1.参数校验
        if(interviewId==null||interviewIsOffer==null){
            throw new RuntimeException("参数为空");
        }
        //2.调用service完成查询
        boolean updateInterview=interviewService.updateInterviewIsOffer(interviewId,interviewIsOffer);
        if(updateInterview){
            return R.success(true,"更新成功");
        }
        return R.error(false,"更新失败");
    }

    //企业删除面试记录
    @Operation(summary = "企业删除面试记录")
    @PostMapping("/deleteInterviewByEnterprise")
    public R<Boolean> deleteInterviewByEnterprise(Long interviewId,Long interviewEnterpriseId){
        //1.参数校验
        if(interviewId==null||interviewEnterpriseId==null){
            throw new RuntimeException("参数为空");
        }
        //2.调用service完成查询
        QueryWrapper<Interview> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("interview_id",interviewId);
        queryWrapper.eq("interview_enterprise_id",interviewEnterpriseId);
        Interview one = interviewService.getOne(queryWrapper);
        one.setEnterpriseIsDeleted("已删除");
        return interviewService.updateById(one)?R.success(true,"删除成功"):R.error(false,"删除失败");
    }
}
